//
//  OnAppearDemo.swift
//  SwiftUIDemo
//
//  Created by Tilt Wang on 2025/7/2.
//

import SwiftUI

struct OnAppearDemo: View {
    
    @State var myText: String = "Start text."
    @State var count: Int = 0
    
    var body: some View {
        NavigationView {
            ScrollView {
                Text(myText)
                LazyVStack {
                    ForEach(0..<50) { index in
                        RoundedRectangle(cornerRadius: 25.0)
                            .frame(height: 200)
                            .padding()
                            .onAppear {
                                myText = "This is the new text! \(index)"
                                count += 1
                            }
//                            .onDisappear {
//                                count -= 1
//                            }
                    }
                }
            }
            .onAppear(perform: {
                DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
                    myText = "This is the new text!"
                }
            })
            .onDisappear(perform: {
                myText = "Ending text."
            })
            .navigationTitle("On Appear Demo: \(count)")
        }
    }
}

#Preview {
    OnAppearDemo()
}
